#!/bin/bash

echo '[info] begin to partition data into training and testing data set...'
if [ -z $1 ]
then
    exit 0
fi
if [ -z $2 ]
then
    exit 0
fi
workingDir=$1
inputFile=$2

TEST_USER_RATIO=20 # 20% user will be taken as testing user
TEST_RATING_RATIO=50 # 50% high rating of testing user will be taken as testing set

function random_100() {
    echo $(($RANDOM % 100))
}

cd ${workingDir}
mkdir -p partitions
curUid=-1
useAsTest=false
while read line
do
    uid=$(echo $line | cut -d" " -f1)
    #echo "[debug] curUid: $curUid, uid: $uid"
    if [ $curUid != $uid ] # meet a new uid, judge whether need to take as testing user
    then
        #echo "[debug] checking equvelance..."
        curUid=$uid
        useAsTest=false
        if [ $(random_100) -lt $TEST_USER_RATIO ] # testing user
        then
            useAsTest=true
            echo "[debug] use $uid as testing user...."
        fi
    fi
    if [ $useAsTest == true ]
    then
        rating=$(echo $line | cut -d" " -f3)
        if [ $rating == 5 ] # check if high rating
        then
            if [ $(random_100) -lt $TEST_RATING_RATIO ]
            then # put into testing set
                echo "[debug] $line is put into testing set..."
                echo $line >> testing.csv
                continue
            else
                echo "[debug] $line is put into training set..."
                echo $line >> training.csv
                continue
            fi
        fi
        echo $line >> training.csv # put those unqualified data into training set
    else # put into training set
        echo $line >> training.csv
    fi
done < $inputFile

echo '[info] complete data partition...'